package cn.sjx.graduate.web.advice;

import cn.sjx.graduate.exception.BusinessException;
import cn.sjx.graduate.exception.LoginException;
import cn.sjx.graduate.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 通用异常处理
 *
 * @Author: bo-feifei
 * @Date 2021/11/12 8:50
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class MyControllerAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public JsonResult<?> handleBusinessException(BusinessException ex) {
//        log.warn("[业务异常] 出现业务异常：", ex);
        ex.printStackTrace();
        return JsonResult.error(ex.getCodeMsg());
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public JsonResult<?> eHandler(Exception e) {
//        log.error(e.getMessage());
        e.printStackTrace();
        return JsonResult.error("系统异常");
    }

    @ExceptionHandler(LoginException.class)
    public JsonResult<?> loginException(LoginException e) {
        e.printStackTrace();
        return JsonResult.error(JsonResult.CODE_NOLOGIN,e.getMessage());
    }
}
